package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.CommonResult;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 李红磊
 * @since 2021-06-13
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;


    //课程大纲列表
    @GetMapping("getChapterVideo/{id}")
    public CommonResult getChapterVideo(@PathVariable("id")String courseId){
        System.out.println("进入到getChapterVideo处理器方法");
        List<ChapterVo> list=chapterService.getChapterVideoService(courseId);
            return CommonResult.ok().data("chapterAndVideo",list);
    }


    //添加章节
    @PostMapping("addChapter")
    public CommonResult addChapter(@RequestBody EduChapter eduChapter){
        boolean save = chapterService.save(eduChapter);
        return save?CommonResult.ok():CommonResult.error();
    }

    //根据章节id查询章节单条
    @GetMapping("queryByIdChapter/{id}")
    public CommonResult queryByIdChapter(@PathVariable("id")String id){
        EduChapter eduChapter = chapterService.getById(id);
        boolean flag=true;
        if(eduChapter==null){
            flag=false;
        }

        return flag?CommonResult.ok().data("eduChapter",eduChapter):CommonResult.error();
    }


    //修改章节
    @PostMapping("updateChapter")
    public CommonResult updateChapter(@RequestBody EduChapter eduChapter ){
        boolean update = chapterService.updateById(eduChapter);
        return update?CommonResult.ok():CommonResult.error();
    }


    //删除章节
    @DeleteMapping("deleteChapter/{id}")
    public CommonResult deleteChapter(@PathVariable("id")String id){
        int count=chapterService.deleteChapter(id);
        return count>0?CommonResult.ok():CommonResult.error();
    }


    
}

